﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Security;
using WmsApp.StaticData.Unity;
using WmsApp.Style;

namespace WmsApp.Window
{
    public partial class BaseForm : DevExpress.XtraEditors.XtraForm, ICanClosing
    {
        /// <summary>
        /// 打开此窗口的用户ID
        /// </summary>
        public string OpenThisFormUserID { get; private set; }

        #region RepositoryItem 2个

        readonly RepositoryItemTextEdit _repTextEdit0Dot00 = null;
        protected RepositoryItem RepTextEdit0Dot00
        {
            get { return _repTextEdit0Dot00; }
        }

        readonly RepositoryItemTextEdit _repTextEdit0Dot0000 = null;
        protected RepositoryItem RepTextEdit0Dot0000
        {
            get { return _repTextEdit0Dot0000; }
        }
        #endregion

        protected string GetAsseblyVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        protected BaseForm()
        {
            InitializeComponent();

            #region Init Control
            //初始化通用公共控件repTextEdit0Dot00           
            this._repTextEdit0Dot00 = new RepositoryItemTextEdit();
            this._repTextEdit0Dot0000 = new RepositoryItemTextEdit();

            this._repTextEdit0Dot00.Name = "repTextEdit0Dot00";
            InitControl(this._repTextEdit0Dot00, "n");

            this._repTextEdit0Dot0000.Name = "repTextEdit0Dot0000";
            InitControl(this._repTextEdit0Dot0000, "n");
            _repTextEdit0Dot0000.DisplayFormat.FormatString = "0.0000";
            _repTextEdit0Dot0000.EditFormat.FormatString = "0.0000";
            #endregion

            try
            {
                this.OpenThisFormUserID = UserSession.CurrentUser.USERID;
            }
            catch
            {}

            InitializeRepository();
        }

        private void InitControl(RepositoryItemTextEdit repTextEdit, string editMask)
        {
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).BeginInit();
            repTextEdit.AutoHeight = false;
            repTextEdit.DisplayFormat.FormatString = "0.00";
            repTextEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            repTextEdit.EditFormat.FormatString = "0.00";
            repTextEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //repTextEdit.Mask.EditMask = "#,##0.00;<<#,##0.00>>";
            repTextEdit.Mask.EditMask = editMask;
            repTextEdit.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            repTextEdit.Mask.UseMaskAsDisplayFormat = true;
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).EndInit();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            //存储grid的样式文件
            SaveDevExpressLayout();
        }

        private List<GridControl> GetGridControls()
        {
            var lst = new List<GridControl>();
            var ctrllst = new List<Control>();
            GetControl(ctrllst, this);
            foreach (Control item in ctrllst)
            {
                if (item is GridControl)
                {
                    lst.Add(item as GridControl);
                }
            }
            return lst;
        }

        /// <summary>
        /// 得到容器里的所有控件
        /// </summary>
        /// <param name="listCtrl"></param>
        /// <param name="ctrl"></param>
        public void GetControl(List<Control> listCtrl, Control ctrl)
        {
            try
            {
                //遍历控件
                foreach (Control ctrl1 in ctrl.Controls)
                {
                    GetControl(listCtrl, ctrl1);
                    listCtrl.Add(ctrl1);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("获得控件失败 " + exception.ToString());
            }
        }

        /// <summary>
        /// 保存布局
        /// </summary>
        private void SaveDevExpressLayout()
        {
            List<GridControl> lst = GetGridControls();
            foreach (GridControl item in lst)
            {
                LayoutHelper.SaveGridLayout(item);
            }
        }

        private void LoadDevExpressLayout()
        {
            List<GridControl> lst = GetGridControls();
            foreach (GridControl item in lst)
            {
                item.Name = item.Name + GetAsseblyVersion();
                LayoutHelper.LoadGridLayout(item);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Text = this.HeadText;
            this.LoadDevExpressLayout();
        }

        public virtual string HeadText 
        {
            get { return ""; }
        }

        RepositoryItemTextEdit repTextEditReadOnly = null;
        protected RepositoryItem RepTextEditReadOnly
        {
            get { return repTextEditReadOnly; }
        }

        RepositoryItemComboBox repComboBox = null;
        protected RepositoryItem RepComboBox
        {
            get { return repComboBox; }
        }

        private void InitializeRepository()
        {
            repTextEditReadOnly= new RepositoryItemTextEdit();
            repTextEditReadOnly.ReadOnly = true;
            repTextEditReadOnly.Appearance.BackColor = Color.Gray;
            repTextEditReadOnly.Appearance.BackColor2 = Color.Gray;
            repTextEditReadOnly.Appearance.Options.UseBackColor = true;

            repComboBox = new RepositoryItemComboBox();
            repComboBox.ReadOnly = false;
        }

    }
}